<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="课堂名称">
          <el-input
            v-model="queryParams.courseName"
            placeholder="课堂名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" placeholder="状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="上架" value="1"></el-option>
            <el-option label="下架" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadCourses">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button
        style="float: right; padding: 15px"
        type="primary"
        @click="$router.push({ name: 'courses-create' })"
        >+ 新建课程</el-button
      >
    </div>
    <el-table :data="queryResult.records" border style="width: 100%">
      <el-table-column prop="id" label="ID" align="center"> </el-table-column>
      <el-table-column prop="courseName" label="课程名称" align="center">
      </el-table-column>
      <el-table-column
        prop="price"
        label="价格"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sortNum"
        label="排序"
        align="center"
      ></el-table-column>
      <el-table-column label="状态" align="center" v-slot="scope">
        <el-switch
          v-model="scope.row.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
          active-text="上架"
          inactive-text="下架"
          @change="handleChangeStatus(scope.row)"
        >
        </el-switch>
      </el-table-column>
      <el-table-column label="操作" align="center" v-slot="scope">
        <el-button
          @click="
            $router.push({
              name: 'courses-edit',
              params: { courseId: scope.row.id },
            })
          "
          >编辑</el-button
        >
        <el-button
          @click="
            $router.push({
              name: 'courses-content',
              params: { courseId: scope.row.id },
            })
          "
          >内容管理</el-button
        >
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryResult.total"
    >
    </el-pagination>
  </el-card>
</template>
<script>
import { getCourse, setCourseStatus } from '@/api/course'
export default {
  name: 'CourseIndex',
  data () {
    return {
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        courseName: '',
        status: undefined
      },
      queryResult: {}
    }
  },
  created () {
    this.loadCourses()
  },
  methods: {
    handleChangeStatus (course) {
      setCourseStatus(course.id, course.status).then((res) => {
        if (res.data.code === '000000') {
          this.$message.success('修改成功')
          this.loadCourses()
        } else {
          this.$message.error('修改失败')
        }
      })
    },
    loadCourses () {
      getCourse(this.queryParams).then((res) => {
        if (res.data.code === '000000') {
          const {
            current: currentPage,
            records,
            size: pageSize,
            total
          } = res.data.data
          Object.assign(this.queryParams, { currentPage, pageSize })
          this.queryResult = {
            pages: Math.ceil(total / pageSize),
            records,
            total
          }
        } else {
          this.$message.error('error')
        }
      })
    },
    handleSizeChange (val) {
      this.queryParams.pageSize = val
      this.queryParams.currentPage = 1
      this.loadCourses()
    },
    handleCurrentChange (val) {
      this.queryParams.currentPage = val
      this.loadCourses()
    }
  }
}
</script>
<style scoped lang="scss">
</style>
